﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-过程检验
    /// </summary>
    public class ProcessServices : ApplicationService, IProcessServices
    {
        private readonly IRepository<Process> _processRepository;

        public ProcessServices(IRepository<Process> processRepository)
        {
            this._processRepository = processRepository;
        }

        /// <summary>
        /// 添加过程检验
        /// </summary>
        /// <param name="processDto"></param>
        /// <returns></returns>
        public async Task<int> AddProcess(ProcessDTO processDto)
        {
            var addprce = ObjectMapper.Map<ProcessDTO, Process>(processDto);
            var i=await _processRepository.InsertAsync(addprce);
            return i == null ? 0 : 1;
        }

        /// <summary>
        /// 显示过程检验
        /// </summary>
        /// <returns></returns>
        public async Task<List<Process>> GetProcess()
        {
            var processlist = await _processRepository.GetListAsync();
            return processlist;
        }
        
        /// <summary>
        /// 显示过程检验
        /// </summary>
        /// <returns></returns>
        public async Task<List<Process>> GetProces()
        {
            var processlist = await _processRepository.GetListAsync();
            return processlist;
        }

        /// <summary>
        /// 根据ID获取过程检验
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Process> GetProcessById(int id)
        {
            return await _processRepository.GetAsync(p => p.Id == id);
        }

        /// <summary>
        /// 修改过程检验
        /// </summary>
        /// <param name="processDto"></param>
        /// <returns></returns>
        public async Task<int> UpdateProcess(ProcessDTO processDto)
        {
            var existingProcess = await _processRepository.GetAsync(p => p.Id == processDto.Id);
            if (existingProcess == null)
            {
                return 0;
            }

            // 更新实体属性
            existingProcess.ProcessCode = processDto.ProcessCode;
            existingProcess.ProcessType = processDto.ProcessType;
            existingProcess.OrderCode = processDto.OrderCode;
            existingProcess.ProductCode = processDto.ProductCode;
            existingProcess.ProductName = processDto.ProductName;
            existingProcess.Specifications = processDto.Specifications;
            existingProcess.Unitt = processDto.Unitt;
            existingProcess.TestingNum = processDto.TestingNum;
            existingProcess.TestingResult = processDto.TestingResult;
            existingProcess.TestingDate = processDto.TestingDate;
            existingProcess.TestingPerson = processDto.TestingPerson;
            existingProcess.State = processDto.State;

            await _processRepository.UpdateAsync(existingProcess);
            return 1;
        }

        /// <summary>
        /// 删除过程检验
        /// </summary>
        /// <param name="ids">要删除的ID列表</param>
        /// <returns></returns>
        public async Task<bool> DeleteProcess(List<int> ids)
        {
            try
            {
                foreach (var id in ids)
                {
                    var process = await _processRepository.GetAsync(p => p.Id == id);
                    if (process != null)
                    {
                        await _processRepository.DeleteAsync(process);
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
